package com.uphone.kingmall.adapter;

import android.app.Activity;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.MotionEvent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.china.zhuoyue.R;
import com.uphone.kingmall.activity.SettleActivity;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.bean.SettleCartBean;
import com.uphone.kingmall.utils.PopupUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/20
 *     desc   :
 * </pre>
 */
public class SettleAdapter0 extends BaseQuickAdapter<SettleCartBean.ShopGoodsBean, BaseViewHolder> {
    private final Activity mActivity;
    private OnShopItemClickListener listener;
    private List<EditText> editTextList = new ArrayList<>();
    int totalHeight = 0;
    int count = 0;

    public SettleAdapter0(Activity activity, OnShopItemClickListener listener) {
        super(R.layout.kingmall_item_rv);
        this.mActivity = activity;
        this.listener = listener;
    }

    @Override
    protected void convert(final BaseViewHolder helper, final SettleCartBean.ShopGoodsBean item) {
        helper.setIsRecyclable(false);
        final LinearLayout ll = helper.getView(R.id.ll);
        if (helper.getLayoutPosition() == 0) {
            totalHeight = 0;
            count = 0;
        }
        ll.post(new Runnable() {
            @Override
            public void run() {
                totalHeight += ll.getMeasuredHeight();
                count++;
                LogUtils.e("count:" + count);
                if (count == getData().size() && mActivity != null && mActivity instanceof SettleActivity) {
                    ((SettleActivity) mActivity).setRecycle(totalHeight);
                }
            }
        });
        helper.setText(R.id.tv_shop_name, item.getShopName() + "");
        RecyclerView rvGood = helper.getView(R.id.rv_good);
        rvGood.setLayoutManager(new LinearLayoutManager(mContext));
        SettleGoodAdapter adapter = new SettleGoodAdapter();
        rvGood.setAdapter(adapter);
        adapter.setNewData(item.getGoods());
        final TextView tvPostFee = helper.getView(R.id.tv_postFee);
        EditText etRemark = helper.getView(R.id.et_remark);
//        etRemark.setFocusableInTouchMode(true);
//        etRemark.requestFocus();
        editTextList.add(etRemark);
        etRemark.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LogUtils.e("点击留言");
                v.setFocusableInTouchMode(true);
                v.requestFocus();
                KeyboardUtils.showSoftInput(mActivity);
                return true;
            }
        });
        tvPostFee.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (!TextUtils.isEmpty(s.toString())) {
                    item.setScoreRealFee(loadScoreDeductFee(helper, item));
                    if (s.toString().contains("快递")) {
                        //快递
                        item.setPost(true);
                    } else {
                        //到店
                        item.setPost(false);
                    }
                    if (listener != null) {
                        listener.calculationPrice(helper.getLayoutPosition(), item);
                    }
                }
            }
        });

        final CheckBox cbBalance = helper.getView(R.id.cb_balance);
        final CheckBox cbIntegral = helper.getView(R.id.cb_integral);

        helper.setText(R.id.tv_balance_price, "¥" + item.getYueDeductFee());
        item.setScoreRealFee(loadScoreDeductFee(helper, item));
        cbBalance.setChecked(false);
        cbIntegral.setChecked(false);
        cbBalance.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cbBalance.setChecked(!item.isYueFeeSelect());
                cbIntegral.setChecked(false);
                item.setYueFeeSelect(cbBalance.isChecked());
                item.setScoreFeeSelect(false);
                if (listener != null) {
                    listener.calculationPrice(helper.getLayoutPosition(), item);
                }
            }
        });

        cbIntegral.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cbIntegral.setChecked(!item.isScoreFeeSelect());
                cbBalance.setChecked(false);
                item.setScoreFeeSelect(cbIntegral.isChecked());
                item.setYueFeeSelect(false);
                if (listener != null) {
                    listener.calculationPrice(helper.getLayoutPosition(), item);
                }
            }
        });

        helper.setText(R.id.tv_postFee, "快递：" + item.getPostFee() + "");

        // 选择消费方式
        tvPostFee.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                View inflate = View.inflate(mActivity, R.layout.kingmall_dialog_settle_way, null);
                final PopupUtil popupFilter = PopupUtil.getInstance(MyApplication.mContext, inflate);
                popupFilter.setBasisView(v).showDirection(PopupUtil.Direction.UNDER);
                inflate.findViewById(R.id.tv_express).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvPostFee.setText("快递：" + item.getPostFee());
                        popupFilter.dismiss();
                    }
                });
                inflate.findViewById(R.id.tv_shop).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        tvPostFee.setText("到店");
                        popupFilter.dismiss();
                    }
                });
                popupFilter.setDismissListener(new PopupWindow.OnDismissListener() {
                    @Override
                    public void onDismiss() {
                        if (popupFilter != null) {
                            popupFilter.release();
                        }
                    }
                });
            }
        });
    }

    /**
     * 计算金币抵扣金额
     *
     * @param helper
     * @param item
     */
    private double loadScoreDeductFee(BaseViewHolder helper, SettleCartBean.ShopGoodsBean item) {
        double realFee = 0;
        double scoreDeductFee = item.getScoreDeductFee();
        double sumGoodsFee = item.getSumGoodsFee();
        if (sumGoodsFee > scoreDeductFee) {
            realFee = scoreDeductFee;
        } else {
            TextView tvPostFee = helper.getView(R.id.tv_postFee);
            if (!TextUtils.isEmpty(tvPostFee.getText().toString()) && tvPostFee.getText().toString().contains("快递")) {
                //快递
                double postFee = item.getPostFee();
                double sumPostFee = sumGoodsFee + postFee;
                if (sumPostFee > scoreDeductFee) {
                    realFee = scoreDeductFee;
                } else {
                    realFee = sumPostFee;
                }
            } else {
                //到店
                realFee = sumGoodsFee;
            }
        }
        item.setScoreRealFee(realFee);
        helper.setText(R.id.tv_integral_price, String.format(Locale.CHINA, "¥%.2f", realFee));
        return realFee;
    }

    public interface OnShopItemClickListener {
        void calculationPrice(int position, SettleCartBean.ShopGoodsBean item);
    }

    public List<EditText> getEditTextList() {
        return editTextList;
    }
}
